package tegneprogram;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Franz
 */
public class Draw {

    private Color c;
    private String status;
    private ArrayList<String> all;
    private ArrayList<Integer> numb;
    private Point Point1;
    private ArrayList<Line> lines;
    private ArrayList<Oval> ovals;
    private ArrayList<Rect> rects;

    public Draw() {
        lines = new ArrayList<>();
        ovals = new ArrayList<>();
        rects = new ArrayList<>();
        all = new ArrayList<>();
        numb = new ArrayList<>();

        status = "line";

        c = Color.white;
    }

    public void doIt(Point bitch) {

        if (Point1 != null) {
            all.add(status);
            if (status.equalsIgnoreCase("line")) {
                newLine(bitch, Point1);
                numb.add(lines.size());
            } else if (status.equalsIgnoreCase("oval")) {
                newOval(bitch, Point1);
                numb.add(ovals.size());
            } else if (status.equalsIgnoreCase("rect")) {
                newRect(bitch, Point1);
                numb.add(rects.size());
            }

            Point1 = null;

        } else {
            Point1 = bitch;
        }
    }

    public void newLine(Point point, Point point2) {
        Line line = new Line(point, point2, c);
        lines.add(line);


    }

    public void newOval(Point point, Point point2) {
        Oval oval = new Oval(point, point2, c);
        ovals.add(oval);
    }

    public void newRect(Point point, Point point2) {
        Rect rect = new Rect(point, point2, c);
        rects.add(rect);



    }

    public void draw(Graphics g) {

        for (int i = 0; i < all.size(); i++) {
            if (all.get(i).equals("line")) {
                lines.get(numb.get(i) - 1).draw(g);
            } else if (all.get(i).equals("oval")) {
                ovals.get(numb.get(i) - 1).draw(g);
            } else if (all.get(i).equals("rect")) {
                rects.get(numb.get(i) - 1).draw(g);
            }

        }


    }

    public String getStatus() {
        return status;
    }

    public void changeColor(Color color) {
        c = color;
    }

    public void setStatus(String status) {
        this.status = status;



    }

    public void removeAll() {
        all = new ArrayList<>();
        numb = new ArrayList<>();
        lines = new ArrayList<>();
        ovals = new ArrayList<>();
        rects = new ArrayList<>();


    }

    public void removeLast() {

        if (!all.isEmpty()) {
            String last = all.get(all.size() - 1);
            all.remove(all.size() - 1);


            if (last.equalsIgnoreCase("line")) {
                if (!lines.isEmpty()) {
                    lines.remove(lines.size() - 1);
                }
            } else if (last.equalsIgnoreCase("oval")) {
                if (!ovals.isEmpty()) {
                    ovals.remove(ovals.size() - 1);
                }
            } else if (last.equalsIgnoreCase("rect")) {
                if (!rects.isEmpty()) {
                    rects.remove(rects.size() - 1);
                }
            }
        }
    }

    public void saveData(String filename) {

        try {
            File file = new File(filename);
            
            PrintWriter pw = new PrintWriter(file);
            for (int i = 0; i < lines.size(); i++) {
                pw.println(lines.get(i));
            }
            for (int i = 0; i < ovals.size(); i++) {
                pw.println(ovals.get(i));
            }
            for (int i = 0; i < rects.size(); i++) {
                pw.println(rects.get(i));

            }

            pw.close();
        } catch (FileNotFoundException ex) {
            System.out.println("ERROR");
        }





    }

    public void LoadSavedData(String string) {
        removeAll();
        try {

            File file = new File(string);
            Scanner scanner = new Scanner(file);
            
            while (scanner.hasNext()) {
                String text = scanner.nextLine();

                String[] texs = text.split(",");
                
                if (Integer.parseInt(texs[0]) == 1) { // type then line
                  all.add("line");

                    changeColor(new Color(Integer.parseInt(texs[5])));
                    newLine(new Point(Integer.parseInt(texs[1]), Integer.parseInt(texs[2])), new Point(Integer.parseInt(texs[3]), Integer.parseInt(texs[4])));
                numb.add(lines.size());
                } else if (Integer.parseInt(texs[0]) == 2) { // type then oval
                    all.add("oval");
                    changeColor(new Color(Integer.parseInt(texs[5])));
                    newOval(new Point(Integer.parseInt(texs[1]), Integer.parseInt(texs[2])), new Point(Integer.parseInt(texs[3]), Integer.parseInt(texs[4])));
numb.add(ovals.size());


                } else if (Integer.parseInt(texs[0]) == 3) { // type then rect


                    changeColor(new Color(Integer.parseInt(texs[5])));
                    newRect(new Point(Integer.parseInt(texs[1]), Integer.parseInt(texs[2])), new Point(Integer.parseInt(texs[3]), Integer.parseInt(texs[4])));
                    all.add("rect");
                    numb.add(rects.size());
                }
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Draw.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
